﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :用于特殊权限指派。
//
//        created by 裘中杰 at  $time$
//======================================================================


using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Controllers;
using Hisun.Models;

namespace Hisun.UserControl.Admin
{
    public partial class AuthsToUser : System.Web.UI.UserControl
    {
        private HisunDBDataContext db = new HisunDBDataContext();
        private string userID;

        public string UserID
        {
            get { return userID; }
            set { userID = value; }
        }
        private bool canEdit;//Allow Edit?

        public bool CanEdit
        {
            get { return canEdit; }
            set { canEdit = value; }
        }

        private bool isInit;//初始化时赋值

        public bool IsInit
        {
            get { return isInit; }
            set { isInit = value; }
        }

        /// <summary>
        /// 获得选择的内容
        /// </summary>
        /// <returns>逗号分割字符串</returns>
        private string GetSelectedValues()
        {
            string[] itemSelected = (from item in chkListAuths.Items.Cast<ListItem>()
                                     where item.Selected
                                     select item.Value).ToArray();
            return string.Join(",", itemSelected);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!canEdit)
            {
                lnkBtnSetAuths.Visible = false;
                lnkBtnSetAuths.Enabled = false;
            }
            if (!isInit)
                throw new Exception("没有初始化!");
            var user = (from r in db.TAbc_JS_Users
                        where r.操作员ID == userID
                        select r).Single();
            labTitle.Text = string.Format(labTitle.Text, user.操作员ID);
            if (!IsPostBack)
            {
                string[] userAuths = user.权限ID.ToString().Split(',').ToArray();
                chkListAuths.DataBind();
                foreach (var item in chkListAuths.Items.Cast<ListItem>())
                {
                    if (userAuths.Contains(item.Value))
                        item.Selected = true;
                }
            }
        }

        protected void lnkBtnSetAuths_Click(object sender, EventArgs e)
        {
            if (!canEdit)
                throw new Exception("没有权限!");
            AbcAccount abcAccount = new AbcAccount(Session[ServerVars.sysSessionUserName].ToString());
            if (abcAccount.SetUserToAuths(userID, GetSelectedValues()) != 0)
            {
                throw new Exception(abcAccount.LastError);
            }
            Response.Write("OK!");
        }

    }
}